<!-- 我的权限 -->

<template>
  <div v-if="auth_readySelect" class="marginBottom">
    <MajorTitle title="权限设置" />

    <EasyRow col3>
      <FormRadio label="权限组类型" bind="meetingPermissionType" :dict="DICT.AUTH" initFirst required />
      <FormRadio label="大门权限" bind="gatePermission" :dict="CORE.DICT.YES" initFirst required />
    </EasyRow>

    <EasyRow v-if="one.meetingPermissionType === 1" col3>
      <FormSelect label="选择权限组" bind="authId, authName" :dict="auth_selectDict" required />
    </EasyRow>

    <FormSlot v-else label="设置权限组" bind="authList" noMargin required>
      <div class="flexCC alignCenter">
        <div class="flex3">
          <b>{{ CORE.MAP.ORGAN }}</b>
        </div>
        <div class="flex2"><b>权限组</b></div>
        <div class="alignCenter" style="width: 100px" />
      </div>

      <draggable v-model="one.authList" :animation="200">
        <div v-for="(item, i) in one.authList" :key="item.id" class="flexCC marginBottom">
          <FormSlot :bind="`child_${item.id}-organ`" :tip="`请选择${CORE.MAP.ORGAN}`" class="flex3" noLabel required>
            <FormTreeMulti
              :one="item"
              bind="organIdList, organNameList"
              :dict="CORE.ORGAN.DICT"
              noLabel
              @change="toChangeOrgan(item)"
            />
          </FormSlot>

          <FormSlot
            :bind="`child_${item.id}-auth`"
            :tip="`请选择${CORE.MAP.ORGAN}`"
            class="flex2"
            :labelWidth="30"
            required
          >
            <FormSelect
              :one="item"
              bind="authId, authName"
              :dict="auth_selectDict"
              noLabel
              @change="toChangeAuth(item)"
            />
          </FormSlot>

          <div class="alignCenter" style="width: 100px">
            <BtnText v-if="!isShow" icon="el-icon-plus" @click="addChild(i)" />
            <BtnText
              v-if="!isShow"
              :disabled="one.authList.length === 1"
              icon="el-icon-minus"
              red
              @click="delChild(i)"
            />
          </div>
        </div>
      </draggable>
    </FormSlot>
  </div>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'MyAuth',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [CORE.mixWeb(), CORE.mixSelect('auth', 'auth_')],

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 子项组织改变
     */
    toChangeOrgan(item) {
      const { one, $set } = this
      const readyStr = item.organIdList.length ? 'ready' : ''
      $set(one, `child_${item.id}_organ`, readyStr)
    },

    /**
     * 子项权限组改变
     */
    toChangeAuth(item) {
      const { one, $set } = this
      const readyStr = item.authId ? 'ready' : ''
      $set(one, `child_${item.id}_auth`, readyStr)
    },

    /**
     * 添加子项
     */
    addChild(i) {
      const { INIT_ONE, one, $set } = this
      const child = CORE.clone(INIT_ONE.authList[0])
      const id = `${Date.now()}`
      $set(one, `child_${id}_organ`, '')
      $set(one, `child_${id}_auth`, '')
      one.authList.splice(i + 1, 0, { ...child, id })
    },

    /**
     * 删除子项
     */
    delChild(i) {
      const { one } = this
      one.authList.splice(i, 1)
    },
  },
}
</script>
